package com.guo.hui.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;


@Controller
@RequestMapping(value = "/tool")
public class ToolExcelController {

    @RequestMapping(value = "/list")
    public String list(){
        return "/upload";
    }

    @RequestMapping(value = "/upload")
    @ResponseBody
    public String upload(@RequestParam("test") MultipartFile file) {
        if (file.isEmpty()) {
            return "文件为空";
        }
        // 获取文件名
        String fileName = file.getOriginalFilename();
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        // 文件上传后的路径
        String filePath = "D://test//";
        // 解决中文问题，liunx下中文路径，图片显示问题
        // fileName = UUID.randomUUID() + suffixName;
        File dest = new File(filePath + fileName);
        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
            return "上传成功";
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "上传失败";
    }

    @RequestMapping(value = "/batchImport")
    @ResponseBody//这个注释加不加都可以实现，加的话跳转重定向到固定页面，不加就是ajax返回
    public String batchImportUserKnowledge(@RequestParam(value="filename") MultipartFile file,
                                           HttpServletRequest request, HttpServletResponse response, HttpSession session
    ) throws IOException{

        //判断文件是否为空
        if(file==null){
            session.setAttribute("msg","文件不能为空！");
            return "redirect:toUserKnowledgeImport";
        }

        //获取文件名
        String fileName=file.getOriginalFilename();
        return "redirect:toUserKnowledgeImport";
    }

    @RequestMapping(value = "/batch/upload", method = RequestMethod.POST)
    @ResponseBody
    public String handleFileUpload(HttpServletRequest request, MultipartFile[] file) {
        //List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        MultipartFile[] files = file;
        MultipartFile filet = null;
        BufferedOutputStream stream = null;
        String filePath = "D://test//";
        for (int i = 0; i < files.length; ++i) {
            filet = files[i];
            if (!filet.isEmpty()) {
                try {
                    byte[] bytes = filet.getBytes();
                    String extName = filet.getOriginalFilename().substring(filet.getOriginalFilename().lastIndexOf("."));
                    stream = new BufferedOutputStream(
                            new FileOutputStream(new File(filePath + System.currentTimeMillis() + extName)));
                    stream.write(bytes);
                    stream.close();

                } catch (Exception e) {
                    stream = null;
                    return "You failed to upload " + i + " => " + e.getMessage();
                }
            } else {
                return "You failed to upload " + i + " because the file was empty.";
            }
        }
        return "upload successful";
    }


    @ResponseBody
    @RequestMapping(value = "/excel")
    public String login(String name){
        return name;
    }

    @ResponseBody
    @RequestMapping(value = "/export")
    public String index(String name){
        return name;
    }

}
